#include "AlapForm.h"

using namespace Osp::Base;
using namespace Osp::Ui;
using namespace Osp::Ui::Controls;
using namespace Osp::Uix;

AlapForm::AlapForm(void)
{
}

AlapForm::~AlapForm(void)
{
}

bool
AlapForm::Initialize()
{
	// Construct an XML form
	Construct(L"IDF_ALAPFORM");

	return true;
}

result
AlapForm::OnInitializing(void)
{
	result r = E_SUCCESS;

	// TODO: Add your initialization code here

	// Get a button via resource ID
	__pButtonOk = static_cast<Button *>(GetControl(L"IDC_BUTTON_OK"));
	if (__pButtonOk != null)
	{
		__pButtonOk->SetActionId(ID_BUTTON_OK);
		__pButtonOk->AddActionEventListener(*this);
	}

	__pLastEventLabel = static_cast<Label *>(GetControl(L"LASTEVENTLABEL"));
	__pSnapLabel = static_cast<Label *>(GetControl(L"SNAPLABEL"));
	__pTapLabel = static_cast<Label *>(GetControl(L"TAPLABEL"));
	__pShakeLabel = static_cast<Label *>(GetControl(L"SHAKELABEL"));

	snaps = 0;
	taps = 0;
	shakes = 0;

	motion = new Motion();
	motion->Construct(*this);
	motion->SetEnabled(MOTION_TYPE_ALL);

	return r;
}

result
AlapForm::OnTerminating(void)
{
	result r = E_SUCCESS;

	return r;
}

void
AlapForm::OnActionPerformed(const Osp::Ui::Control& source, int actionId)
{
	switch(actionId)
	{
	case ID_BUTTON_OK:
		{
			snaps = taps = shakes = 0;
			__pLastEventLabel->SetText("");
			__pShakeLabel->SetText(Osp::Base::Integer::ToString(shakes));
			__pSnapLabel->SetText(Osp::Base::Integer::ToString(snaps));
			__pTapLabel->SetText(Osp::Base::Integer::ToString(taps));
		}
		break;
	default:
		break;
	}
}

void
AlapForm::OnDoubleTapDetected()
{
	__pLastEventLabel->SetText("tap");
	taps++;
	__pTapLabel->SetText(Osp::Base::Integer::ToString(taps));

}

void
AlapForm::OnShakeDetected(MotionState motionState)
{
 if(motionState==MOTION_STARTED)
 {
	 __pLastEventLabel->SetText("shake");
	shakes++;
	__pShakeLabel->SetText(Osp::Base::Integer::ToString(shakes));

 }


 // Works:
 Draw();
 Show();
}
void
AlapForm::OnSnapDetected(Osp::Uix::MotionSnapType snapType)
{
 switch (snapType) {
	case MOTION_SNAP_X_NEGATIVE:
		__pLastEventLabel->SetText("snap, x-");
		snaps++;
		__pSnapLabel->SetText(Osp::Base::Integer::ToString(snaps));
		break;
	case MOTION_SNAP_Y_NEGATIVE:
		__pLastEventLabel->SetText("snap, y-");
		snaps++;
		__pSnapLabel->SetText(Osp::Base::Integer::ToString(snaps));
		break;
	case MOTION_SNAP_Z_NEGATIVE:
		__pLastEventLabel->SetText("snap, z-");
		snaps++;
		__pSnapLabel->SetText(Osp::Base::Integer::ToString(snaps));
		break;
	case MOTION_SNAP_X_POSITIVE:
		__pLastEventLabel->SetText("snap, x+");
		snaps++;
		__pSnapLabel->SetText(Osp::Base::Integer::ToString(snaps));
		break;
	case MOTION_SNAP_Y_POSITIVE:
		__pLastEventLabel->SetText("snap, y+");
		snaps++;
		__pSnapLabel->SetText(Osp::Base::Integer::ToString(snaps));
		 break;
	case MOTION_SNAP_Z_POSITIVE:
		__pLastEventLabel->SetText("snap, z+");
		snaps++;
		__pSnapLabel->SetText(Osp::Base::Integer::ToString(snaps));
		break;
	default:
		break;
}

 Draw();
 Show();
}
